package top.mable.payment.controller;

import cn.hutool.json.JSONUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.mable.common.vo.DemoMsg;
import top.mable.payment.service.OrderService;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author wen
 * @Date 2021/7/6
 **/

@RestController
public class MQController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private OrderService orderService;

    /**
     * 普通队列
     * @param map
     */
    @RequestMapping("/send")
    public void send(@RequestBody Map<String,String> map){
        DemoMsg msg = new DemoMsg();
        msg.setId(1);
        msg.setContent(JSONUtil.toJsonStr(map));
        msg.setClassName("MQController");
        msg.setMethod("send");
        msg.setRoutingKey("demo.demo");
        msg.setExchange("demo-exchange");
        rabbitTemplate.convertAndSend("demo-exchange","demo.demo",msg);
    }


    /**
     * 测试延时队列
     * @param map
     */
    @RequestMapping("/createOrder")
    public void createOrder(@RequestBody Map<String,Object> map){


        HashMap<String,Object> args = (HashMap) map;
        String productId = (String) args.get("productId");
        String userId = (String) args.get("userId");

        int num = (int) args.get("num");

        orderService.makeOrder(userId,productId,num);

    }

}
